package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.pecvd_1_report.PECVD1ReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IPECVD1ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;


@Slf4j
@Service
public class PECVD1ReportServiceImpl implements IPECVD1ReportService {


    public List<PECVD1ReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        //舟号、舟次数、管号、舟位置、膜厚值、等级、
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("substring(A.data_value_389, 3, 3) AS zhNumber,");
        sqlVo.appendSql("substring(A.data_value_389, 7, 3) AS zhCount,");
        //sqlVo.appendSql("substring(A.data_value_389, 10, 1) AS zhPlace,");
        sqlVo.appendSql("substring(A.data_value_389, 6, 1) AS pipeNumber,");
        sqlVo.appendSql("A.data_value_338 AS filmThickness,");
        sqlVo.appendSql("A.data_value_001 AS grade,");
        sqlVo.appendSql("A.rpt_timestamp");
        sqlVo.appendSql("FROM icim_jn.spc_ret_mfg_data_pecvd A");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND A.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getPipe_number())) sqlVo.appendSql("AND substring(A.data_value_389, 6, 1) in" + jointInSql(conditions.getPipe_number()));
        if (StringUtils.isNotEmpty(conditions.getZh_number())) sqlVo.appendSql("AND substring(A.data_value_389, 3, 3) in" + jointInSql(conditions.getZh_number()));
        if (StringUtils.isNotEmpty(conditions.getZh_count())) sqlVo.appendSql("AND substring(A.data_value_389, 7, 3) in" + jointInSql(conditions.getZh_count()));
        if (StringUtils.isNotEmpty(conditions.getZh_place())) sqlVo.appendSql("AND substring(A.data_value_389, 10, 1) in" + jointInSql(conditions.getZh_place()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND RPT_TIMESTAMP >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND RPT_TIMESTAMP < ?", conditions.getEnd_time());
        sqlVo.appendSql("GROUP BY TOOL_ID_FK, DATA_VALUE_338, DATA_VALUE_389");
        sqlVo.appendSql("ORDER BY RPT_TIMESTAMP DESC");

        return mySql_QueryRunner_QueryListBySql(sqlVo, PECVD1ReportGridOut.class);
    }

}
